<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" :title="isAdd ? '新增机构' : '修改机构'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="160px">
      <!-- <el-form-item label="机构类别" prop="busMainData.orgType">
        <el-radio-group v-model="form.busMainData.orgType">
          <el-radio v-for="item in dicts.org_type" :label="item.dictCode" :key="item.dictCode" >{{ item.dictName }}</el-radio>
        </el-radio-group>
      </el-form-item>-->
      <el-form-item label="名称" prop="busMainData.orgName">
        <el-input v-model="form.busMainData.orgName" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="编号" prop="busMainData.thirdCode">
        <el-input v-if="form.busMainData.orgType != '2'" v-model="form.busMainData.thirdCode" style="width: 300px;"/>
        <el-input v-if="form.busMainData.orgType == '2'" :readonly="true" v-model="form.busMainData.thirdCode" placeholder="请选择用户">
          <el-button slot="append" icon="el-icon-search" @click="selectUser('thirdCode')" />
        </el-input>
      </el-form-item>
      <el-form-item v-if="form.busMainData.orgType !== '2'" label="机构负责人" prop="busMainData.mgrAccount">
        <el-input :readonly="true" v-model="form.busMainData.mgrAccount" placeholder="请选择用户">
          <el-button slot="append" icon="el-icon-search" @click="selectUser('mgrAccount')" />
        </el-input>
      </el-form-item>
      <el-form-item style="margin-bottom: 0px;" label="上级机构" prop="busMainData.parentId">
        <OrgsetTreeSelect v-model="form.busMainData.parentId" :has-priv="false" :cur-org-id="form.busMainData.id" value-field="id" style="width: 300px;" placeholder="选择上级机构" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','ORGSET_ALL','ORGSET_CREATE']" :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
    <userSelectList ref="singleUserSelectList" :mult_select="false" @userSelectCallback="userSelectCallback" />
  </el-dialog>
</template>

<script>
import { addOrEdit, viewMainData, toAdd, toCopy } from '@/api/system/orgSet'
import userSelectList from '@/views/system/user/module/userSelectList'
import OrgsetTreeSelect from '@/views/components/OrgsetTreeSelect'

export default {
  components: { userSelectList, OrgsetTreeSelect },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  dictTypes: ['org_type'],
  data() {
    return {
      loading: false,
      dialog: false,
      isAdd: false,
      userSelectType: '',
      form: {
        busMainData: {
          id: null,
          orgName: '',
          orgType: '1', // 默认部门
          orgStatus: '1',
          thirdCode: '',
          enableFlag: 'Y',
          mgrAccount: '',
          parentId: ''
        }
      },
      rules: {
        'busMainData.orgName': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ],
        'busMainData.thirdCode': [
          { required: true, message: '编号', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    addData(parentId) {
      this.isAdd = true
      const data = { paramBean: { }, busMainData: { parentId: parentId }}
      toAdd(data).then(res => {
        this.dialog = true
        this.form = res.data
        if (!this.form.busMainData.parentId) {
          this.form.busMainData.parentId = null
        }
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      toCopy(data).then(res => {
        this.dialog = true
        this.form = res.data
        if (!this.form.busMainData.parentId) {
          this.form.busMainData.parentId = null
        }
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      this.dialog = true
      const data = { paramBean: { id: id }}
      viewMainData(data).then(res => {
        this.form = res.data
        // 将 '' 置为 空值
        if (!this.form.busMainData.parentId) {
          this.form.busMainData.parentId = null
        }
      })
    },
    selectUser(userSelectType) {
      this.userSelectType = userSelectType
      const _this = this.$refs.singleUserSelectList
      _this.init()
      _this.dialog = true
    },
    userSelectCallback(selectedRow, userSelectObj) {
      if (selectedRow) {
        if (this.userSelectType === 'mgrAccount') {
          this.form.busMainData.mgrAccount = selectedRow.userAccount
        } else {
          this.form.busMainData.thirdCode = selectedRow.userAccount
          this.form.busMainData.orgName = selectedRow.userName
        }
      }
      userSelectObj.cancel()
      userSelectObj.loading = false
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
